package com.app.controller.miniprogram;

import com.app.common.Result;
import com.app.model.StudyRecordModel;
import com.app.model.UserModel;
import com.app.service.StudyService;
import com.app.service.UserService;
import com.app.util.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Controller
@RestController
@RequestMapping("/api/study")
public class StudyController {
    @Autowired
    private StudyService studyService;
    @Autowired
    private UserService userService;

    //分类列表
    @GetMapping("/selectTypeList")
    public Result<Object> selectTypeList() {
        return Result.success(studyService.selectTypeList());
    }

    //列表查询，通过分类、标题关键字筛选
    @GetMapping("/selectStudyList")
    public Result<Object> selectStudyList(Integer type, String title, Integer page, Integer limit,Integer villageId) {
        return Result.success(studyService.selectStudyList(type, title, page, limit,villageId));
    }

    //查看详情
    @GetMapping("/selectContentById")
    public Result<Object> selectContentById(Integer id, HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token != null) {
            String mobile = TokenUtils.nothingness(token);
            UserModel user = userService.getUserByOpenid(mobile);
            if (user == null) {
                return Result.error(405, "用户不存在");
            }
            Integer userId = user.getId();
            return Result.success(studyService.selectContentById(id, userId));
        }
        if (id == null || id == 0) {
            return null;
        }
        return Result.success(studyService.selectContentById(id));
    }

    // 学习记录  Record
    @PostMapping("/insertStudyRecord")
    public Result<Object> insertStudyRecord(@RequestBody StudyRecordModel model) {
        studyService.insertStudyRecord(model);
        return Result.success("操作成功");
    }
}
